package com.h2210c.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.h2210c.user.mapper.TeamMapper;
import com.h2210c.user.mapper.XuanxiangMapper;
import com.h2210c.user.service.PaperService;
import com.jiyun.model.Paper;
import com.jiyun.model.Team;
import com.jiyun.model.Xuanxiang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("kaishijingsai")
@RestController
public class KaishijiangsaiController {
@Autowired
    PaperService paperService;
@Autowired
    XuanxiangMapper xuanxiangMapper;

@Autowired
    TeamMapper teamMapper;
@RequestMapping("team")
public List<Team>team(){
    return teamMapper.selectList(null);
}


    @RequestMapping("findPage")
    public PageInfo<Paper>findPage(@RequestBody Paper paper){
        return paperService.findPage(paper);

    }
    @RequestMapping("xuanxiangbypid")
    public List<Xuanxiang>xuanxiangbypid(@RequestParam Integer id){

        if (id==null){
            id=1;
        }
        QueryWrapper<Xuanxiang> xuanxiangQueryWrapper = new QueryWrapper<>();
        xuanxiangQueryWrapper.lambda().eq(Xuanxiang::getPid,id);
        return xuanxiangMapper.selectList(xuanxiangQueryWrapper);
    }
        @RequestMapping("xuanxiangupdate")
    public void xuanxiangupdate(@RequestParam Integer id){
        Xuanxiang xuanxiang = xuanxiangMapper.selectById(id);
        Paper paper = paperService.getById(xuanxiang.getPid());
        paper.setTihao(id);
        paperService.updateById(paper);
    }

@RequestMapping("timulist")
    public List<Paper>timulist(){
        return paperService.list();
}


@RequestMapping("paihangbang")
public List<Team>paihangbang(){
    QueryWrapper<Team> teamQueryWrapper = new QueryWrapper<>();
    teamQueryWrapper.lambda().orderBy(true,false,Team::getFenshu);
  return   teamMapper.selectList(teamQueryWrapper);
}
@RequestMapping("paiming")
public Integer paiming(@RequestParam Integer id){
    Team team = teamMapper.selectById(id);
    Integer fenshu= team.getFenshu();
int a=1;
    List<Team> list = teamMapper.selectList(null);
    for (Team team1 : list) {
        if (team1.getFenshu()>fenshu){
            a=a+1;
        }
    }
    return a;

}

    @RequestMapping("fenshu")
    public Integer fenshu(@RequestParam Integer id){
        Team team = teamMapper.selectById(id);

        List<Paper> list = paperService.list();
        int a=0;
        for (Paper paper : list) {

            if (paper.getTihao().equals(paper.getDaantihao())){
                a+=20;
            }
            paper.setTihao(0);
            paperService.updateById(paper);

        }

team.setFenshu(a);
        teamMapper.updateById(team);
        return a;

    }
}
